package org.ling.xuan.pannel;

import cn.hutool.core.util.StrUtil;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.components.JBTextArea;
import com.intellij.ui.components.JBTextField;
import org.ling.xuan.config.SysConstant;
import org.ling.xuan.config.SystemProperties;
import org.ling.xuan.entity.mo.EmailInitInfo;
import org.ling.xuan.entity.mo.EmailSendInfo;
import org.ling.xuan.util.LxOptional;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Date;

public class EmailPreviewPanel extends JBPanel {
    private final JBTextField recipientField = new JBTextField(80);
    private final JBTextField copyRec = new JBTextField(80);
    private final JBTextField subjectField = new JBTextField(80);
    private final JBTextArea contentArea = new JBTextArea(20, 120);
    private final JBTextArea attachmentList = new JBTextArea(5, 80);
    private final EmailInitInfo emailInitInfo;

    public EmailPreviewPanel(EmailInitInfo emailInitInfo) {
        setLayout(new GridBagLayout());
        this.emailInitInfo = emailInitInfo;
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.WEST;
        constraints.insets = new Insets(5, 5, 5, 5);

        // 创建收件人输入框
        add(new JLabel("收件人: "), constraints);
        recipientField.setText(emailInitInfo.getCommonSetting().getEmailContact());
        constraints.gridx = 1;
        constraints.gridwidth = 3;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        add(recipientField, constraints);


        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        constraints.fill = GridBagConstraints.NONE;
        add(new JLabel("抄送人: "), constraints);
        constraints.gridx = 1;
        constraints.gridwidth = 3;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        add(copyRec, constraints);

        // 创建邮件标题输入框
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.fill = GridBagConstraints.NONE;
        add(new JLabel("标题: "), constraints);
        String title = SystemProperties.getOrDefaultStr(SysConstant.emailTitlePre, "【代码提交】基金3.0代码提交   ") + emailInitInfo.getAddInfo().getAddMsg();
        subjectField.setText(title);
        constraints.gridx = 1;
        constraints.gridwidth = 3;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        add(subjectField, constraints);

        // 创建邮件内容文本编辑区域
        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 4;
        constraints.gridheight = 4;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1.0;
        constraints.weighty = 1.0;
        contentArea.setText(emailInitInfo.getMsg());
        JBScrollPane contentScrollPane = new JBScrollPane(contentArea);
        add(contentScrollPane, constraints);

        // 创建附件列表
        constraints.gridx = 0;
        constraints.gridy = 7;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.fill = GridBagConstraints.NONE;
        constraints.weightx = 0.0;
        constraints.weighty = 0.0;
        attachmentList.setText(StrUtil.join("\n", LxOptional.of(emailInitInfo.getFiles()).orElse(new ArrayList<>())));
        JBScrollPane attachmentScrollPane = new JBScrollPane(attachmentList);
        add(new JLabel("附件: "), constraints);

        constraints.gridx = 1;
        constraints.gridwidth = 3;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        add(attachmentScrollPane, constraints);
    }

    public EmailSendInfo getInfo() {
        EmailSendInfo emailSendInfo = new EmailSendInfo();
        if(StrUtil.isNotBlank(recipientField.getText())) {
            emailSendInfo.setReviver(StrUtil.split(recipientField.getText(), ";"));
        }else{
            emailSendInfo.setNoticer(new ArrayList<>());
        }
        if(StrUtil.isNotBlank(copyRec.getText())) {
            emailSendInfo.setNoticer(StrUtil.split(copyRec.getText(), ";"));
        }else{
            emailSendInfo.setNoticer(new ArrayList<>());
        }
        emailSendInfo.setSender(emailInitInfo.getEmailSetting().getEmail());
        if(StrUtil.isNotBlank( attachmentList.getText())) {
            emailSendInfo.setAttachList(StrUtil.split(StrUtil.replace(attachmentList.getText().trim(), ";", "\n"), "\n"));
        }else{
            emailSendInfo.setAttachList(new ArrayList<>());
        }
        emailSendInfo.setMsg(LxOptional.of(contentArea.getText()).orElse(""));
        emailSendInfo.setTitle(LxOptional.of(subjectField.getText()).orElse(""));
        emailSendInfo.setDate(new Date());
        return emailSendInfo;
    }
}
